<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            background: skyblue;
            position: absolute;
        }
    </style>
</head>
<body>
    <button>去</button>
    <button>回</button>
    <div class="box"></div>
    <script>
        let getStyle=(obj,attr)=>{
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }
            return window.getComputedStyle(obj,null)[attr];
        }

        let aniamte=(oEle,attr,target)=>{
            clearInterval(oEle.timer);
            oEle.timer=setInterval(()=>{
                let current=parseInt(getStyle(oEle,attr));
                // 步长=(目标值-当前位置)/10
                let steps=(target-current)>0?Math.ceil((target-current)/10):Math.floor((target-current)/10);
                // 新的位置=当前位置+步长'
                oEle.style[attr]=current+steps+"px";
                // 到家
                if(current==target){
                    clearInterval(oEle.timer)
                }
            },20)
        }

        let oBtns=document.querySelectorAll("button");
        let oBox=document.querySelector(".box");
        oBtns[0].onclick=()=>{
            aniamte(oBox,"left",500);
        }
        oBtns[1].onclick=()=>{
            aniamte(oBox,"left",5);
        }
    </script>
</body>
</html>